package com.example.pucha_xian.common;

import java.io.File;
import java.util.Map;
import java.util.Set;

import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class OKHttp {
    private static final MediaType MEDIA_TYPE = MediaType.parse("image/jpg");

    static void upload(String address, String token, Map<String, String> map, Callback callback){
        OkHttpClient client = new OkHttpClient();
        //RequestBody:multipartBody, 多类型 （用户名、密码、头像）
        MultipartBody.Builder builder = new MultipartBody.Builder();

        Set<String> keys = map.keySet();
        for (String key: keys) {
            String value = map.get(key);
            if(value != null){
                if(value.contains("jpg")) {
                    File file = new File(PhotoConfig.sdCard + PhotoConfig.PHOTO_PATH + value);
                    if (file.exists()) {
                        builder.addFormDataPart("image" + key, value, RequestBody.create(MEDIA_TYPE, file));
                    }
                }
                builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"" + key + "\""),
                        RequestBody.create(null, value));
            }
        }

        MultipartBody body = builder.build();
        Request request = new Request.Builder()
                .url(address)
                .addHeader("BZ-Token",token)
                .post(body)
                .build();
        client.newCall(request).enqueue(callback);
    }


    public static void postData(String address, String token, String name, String city, String country, String status, Callback callback){
        OkHttpClient client = new OkHttpClient();
        HttpUrl  url = HttpUrl.parse(address).newBuilder()
                .addQueryParameter("username",name)
                .addQueryParameter("country",country)
                .addQueryParameter("city",city)
                .addQueryParameter("status",status)
                .build();
        FormBody.Builder builder = new FormBody.Builder();
        FormBody body = builder.build();
        Request request = new Request.Builder()
                .url(url)
                .addHeader("BZ-Token",token)
                .post(body)
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void postBackPoints(String address, String token, String  username,String status, Callback callback){
        OkHttpClient client = new OkHttpClient();
        HttpUrl  url = HttpUrl.parse(address).newBuilder()
                .addQueryParameter("username",username)
                .addQueryParameter("status",status)
                .build();
        FormBody.Builder builder = new FormBody.Builder();
        FormBody body = builder.build();
        Request request = new Request.Builder()
                .url(url)
                .addHeader("BZ-Token",token)
                .post(body)
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void postToken(String address, String name, String password, String id, Callback callback){
        OkHttpClient client = new OkHttpClient();
        HttpUrl  url = HttpUrl.parse(address).newBuilder()
                .addQueryParameter("username",name)
                .addQueryParameter("password",password)
                .addQueryParameter("id",id)
                .build();
        FormBody.Builder builder = new FormBody.Builder();
        FormBody body = builder.build();
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        client.newCall(request).enqueue(callback);
    }

}
